package nl.boelen.game.entities;

import nl.boelen.game.client.Client;
import nl.boelen.game.level.tile.Tile;
import nl.boelen.game.options.Option;

public abstract class Mob extends Entity {

    protected byte movingDirection = Option.DOWN;
    protected byte sendMovingDirection = Option.DOWN;
    protected String name;
    protected int speed;
    protected int numSteps = 0;
    protected int scale = 1;
    protected boolean isMoving;

    public Mob(Client client, String name, int x, int y, int speed) {
        super(client);
        this.name = name;
        this.speed = speed;
        this.x = x;
        this.y = y;
    }

    public void move(int xa, int ya) {
        if (xa != 0 && ya != 0) {
            move(xa, 0);
            move(0, ya);
            numSteps--;
            return;
        }
        numSteps++;

        if (ya < 0) {
            movingDirection = Option.UP;
        } else if (ya > 0) {
            movingDirection = Option.DOWN;
        }

        if (xa < 0) {
            movingDirection = Option.LEFT;
        } else if (xa > 0) {
            movingDirection = Option.RIGHT;
        }

        if (!hasCollided(xa, ya)) {
            x += xa * speed;
            y += ya * speed;
        }
    }

    protected boolean isSolidTile(int xa, int ya, int x, int y) {
        if (client.level == null) {
            return false;
        }

        Tile lastTile = client.level.getTile((this.x + x) >> 3, (this.y + y) >> 3);
        Tile newTile = client.level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);

        if (!lastTile.equals(newTile) && newTile.isSolid()) {
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }
    
    public abstract boolean hasCollided(int xa, int ya);
}
